
package ro.aiesec.jdbc_class;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class DBManager {
	
	static private String driverName="com.mysql.jdbc.Driver";
	static private String dbEngine ="mysql";
	static private String host = "85.120.204.235";
	static private String port = "3306";
	static private String database ="enterprise_db";
	static private String user = "enterprise";
	static private String password ="enterprise";
	static private Connection connection = getConnection();
	
	/**
	 * @param sToFilter
	 * @return
	 */
	public static String toSQLString(String sToFilter)
	{
		sToFilter = sToFilter.replaceAll("[-#'\"\\\\]","\\\\$0");
		return sToFilter;
	}
	
	/**
	 * @return
	 */
	public static Connection getConnection(){
		if (connection != null)
			return connection;
		
		Connection connection = null;
		try
		{
			Class.forName(driverName);
		}catch(ClassNotFoundException e)
		{
		
			System.out.println("Connector not found");
			e.printStackTrace();
		}
			
		try
		{
			connection = DriverManager.getConnection(
						"jdbc:"+dbEngine+"://"+host+":"+port+"/"+database,
						user,
						password
					
					);
		}catch(SQLException e){
			System.out.println("Connection failed");
			e.printStackTrace();
			connection=null;
		}
		
		if(connection != null)
		{
			System.out.println("Database connection successfull!");
		}
		else
		{
			System.out.println("Database connection Failed!");
		}
		return connection;
	}

}
